package com.tangcco.lockwell.controller.student;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.domain.TopicNalysis;
import com.tangcco.lockwell.domain.dto.TopicNalysisDto;
import com.tangcco.lockwell.domain.dto.politics.QuestionPoliticsChildInfoDto;
import com.tangcco.lockwell.domain.enums.CheckTypeEnum;
import com.tangcco.lockwell.service.QuestionService;
import com.tangcco.lockwell.service.TopicNalysisService;
import com.tangcco.lockwell.utility.AiUtil;
import com.tangcco.lockwell.utility.DateTimeUtil;
import com.tangcco.lockwell.utility.JsonUtil;
import com.tangcco.lockwell.utility.StudentSubjectUtil;
import com.tangcco.lockwell.viewmodel.admin.question.QuestionVm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 题目解析
 *
 * @author yanxinping
 * @date 2025-01-06 16:43
 */
@RestController
@RequestMapping("/api/student/topNalysis")
public class TopNalysisController {
    @Autowired
    private TopicNalysisService topicNalysisService;
    @Autowired
    private StudentSubjectUtil studentSubjectUtil;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private AiUtil aiUtil;


    /**
     * 获取长难句信息
     * @param currentQuestionIndex
     * @return
     */
    @RequestMapping("/getLongDifficultSentenceInfo")
    public RestResponse<TopicNalysisDto> getLongDifficultSentenceInfo(@RequestParam(name = "currentQuestionIndex")Integer currentQuestionIndex) {
        TopicNalysisDto topicNalysis = new TopicNalysisDto();
        //查询所有长难句
        topicNalysis.setStatus(2);
        Integer subjectId = studentSubjectUtil.getSubjectIdByUser("english");
        topicNalysis.setSubjectId(subjectId);
        List<TopicNalysis> resultList = topicNalysisService.selectByCondition(topicNalysis);
        TopicNalysisDto result = new TopicNalysisDto();
        if(CollectionUtil.isNotEmpty(resultList)){
            result = BeanUtil.copyProperties(resultList.get(currentQuestionIndex), TopicNalysisDto.class);
            result.setTotalNumber(resultList.size());
            return RestResponse.ok(result);
        }
        result.setTotalNumber(0);
        return RestResponse.ok(result);
    }


    /**
     * 政治题目解析
     * @param questionId
     * @return
     */
    @RequestMapping("/analysticTopicForPolitics")
    public RestResponse<TopicNalysisDto> analysticTopicForPolitics(@RequestParam(name = "questionId")Integer questionId){
        QuestionVm questionVm = questionService.selectById(questionId);
        if(questionVm == null){
            return RestResponse.ok(new TopicNalysisDto());
        }
        TopicNalysisDto topicNalysis = new TopicNalysisDto();
        topicNalysis.setQuestionId(questionId);
        List<TopicNalysis> resultList = topicNalysisService.selectByCondition(topicNalysis);
        if(CollectionUtil.isNotEmpty(resultList)){
            return RestResponse.ok(BeanUtil.copyProperties(resultList.get(0), TopicNalysisDto.class));
        }else{
            if(questionVm.getTextContent() != null){
                List<QuestionPoliticsChildInfoDto> questionList = JsonUtil.toJsonListObject(questionVm.getTextContent(), QuestionPoliticsChildInfoDto.class);
                StringBuilder questionBuilder = new StringBuilder();
                if(StringUtils.isEmpty(questionVm.getCorrect()) && CollectionUtil.isNotEmpty(questionList)){
                    questionBuilder.append(questionVm.getArticleContent());
                    questionBuilder.append(questionList.stream().map(QuestionPoliticsChildInfoDto::getQtitle).reduce((a, b) -> a + " " + b).orElse(""));
                }else if(questionVm.getCorrect().split(",").length==1 && CollectionUtil.isNotEmpty(questionList)){
                    questionBuilder.append(questionList.get(0).getQtitle());
                    questionBuilder.append(questionList.get(0).getItem().stream().map(q -> q.getPrefix()+"."+q.getContent()).collect(Collectors.joining(" ")));
                }else if(questionVm.getCorrect().split(",").length>1 && CollectionUtil.isNotEmpty(questionList)){
                    questionBuilder.append(questionList.get(0).getQtitle());
                    questionBuilder.append(questionList.get(0).getItem().stream().map(q -> q.getPrefix()+"."+q.getContent()).collect(Collectors.joining(" ")));
                }
                JsonNode jsonNode = aiUtil.politicsAnalyzeQuestion(questionBuilder.toString(), questionVm.getCorrect());
                if(jsonNode!= null){
                    TopicNalysis newInfo = new TopicNalysis();
                    newInfo.setQuestionId(questionId);
                    newInfo.setSubjectMatter(questionBuilder.toString());
                    newInfo.setAnalysis(jsonNode.toString());
                    newInfo.setStatus(0);
                    newInfo.setCreateTime(DateTimeUtil.dateFormat(new Date()));
                    topicNalysisService.insertByFilter(newInfo);
                    return RestResponse.ok(BeanUtil.copyProperties(newInfo, TopicNalysisDto.class));
                }
            }
        }
        return RestResponse.ok(new TopicNalysisDto());
    }
}

